package com.secneo.appshare.threads;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.mime.qweibo.QWeiboSyncApi;
import com.secneo.appshare.common.Constants;

public class LoginQWeiboThread extends Thread{

	private Context ctx = null ;
	private Handler handler = null ;
	
	private String mTokenKey = null ;
	private String mTokenSecret = null ;
	
	public LoginQWeiboThread(Context ctx , Handler handler){
		this.ctx = ctx ;
		this.handler = handler ;
	}
	
	public void run(){
		QWeiboSyncApi api = new QWeiboSyncApi();
		String resStr = api.getRequestToken(Constants.QWEIBO_APP_KEY, 
				Constants.QWEIBO_APP_SECRET);
		
		Log.d("qq" , "get request_token str" + resStr) ;
		
		parseToken(resStr , mTokenKey , mTokenSecret) ;
		
		Message msg = new Message() ;
		msg.what = 1 ;
		Bundle data = new Bundle() ;
		data.putString(Constants.QWEIBO_TOKEN_ACTION, mTokenKey) ;
		data.putString(Constants.QWEIBO_TOKEN_SECRET_ACTION, mTokenSecret) ;
		msg.setData(data) ;
		handler.sendMessage(msg) ;
	}
	
	//for qweibo
	public boolean parseToken(String response , 
			String tokenKey , String tokenSecret) {
		if (response == null || response.equals("")) {
			return false;
		}

		String[] tokenArray = response.split("&");

		if (tokenArray.length < 2) {
			return false;
		}

		String strTokenKey = tokenArray[0];
		String strTokenSecrect = tokenArray[1];

		String[] token1 = strTokenKey.split("=");
		if (token1.length < 2) {
			return false;
		}
		mTokenKey = token1[1];

		String[] token2 = strTokenSecrect.split("=");
		if (token2.length < 2) {
			return false;
		}
		mTokenSecret = token2[1];

		return true;
	}
	
}
